﻿@page "/Account/ChangePassword"
@rendermode InteractiveServer
@using Microsoft.AspNetCore.Components.Authorization
@using HtERP.Data
@using System.Security.Cryptography
@using System.Text
@inject AuthenticationStateProvider AuthStateProvider
@inject NavigationManager NavigationManager

<PageTitle>Change Password</PageTitle>

<h2>修改密码</h2>
<p>你好:@AuthState?.User?.Identity?.Name</p>
<p>手机号:@phone</p>
 <h3 style="color: #AA0000;">@logz</h3>

<div class="row">
    <div class="col-md-4">
      <hr />

        <div class="form-floating mb-3">
            <InputText type="password" @bind-Value="@OldPassword" class="form-control" autocomplete="current-password" aria-required="true" placeholder="password" />
            <label for="password" class="form-label">旧密码</label>

        </div>

        <div class="form-floating mb-3">
            <InputText type="password" @bind-Value="@NewPassword" class="form-control" autocomplete="current-password" aria-required="true" placeholder="password" />
            <label for="password" class="form-label">新密码</label>

        </div>
        <div class="form-floating mb-3">
            <InputText type="password" @bind-Value="@RPassword" class="form-control" autocomplete="current-password" aria-required="true" placeholder="password" />
            <label for="password" class="form-label">再次输入新密码</label>

        </div>

        <div>
            <button @onclick="@ChangePwd" class="w-100 btn btn-lg btn-primary">确 定</button>

        </div>
       
    </div>

</div>

@code {

    string OldPassword = "";
    string NewPassword = "";
    string RPassword = "";
    string logz = "";
    string phone = "";

    [CascadingParameter]
    private Task<AuthenticationState>? authenticationState { get; set; }
    AuthState? AuthState;
    protected override async Task OnInitializedAsync()
    {
        AuthState = authenticationState?.Result as AuthState;
        phone = AuthState.Phone;
        await InvokeAsync(StateHasChanged);
    }

    private void ChangePwd()
    {
        if (OldPassword == "" | NewPassword == "" | RPassword == "")
        {
            logz = "密码不能为空，请输入！";
        }
        else
        {
            string pwd = Convert.ToHexString(MD5.HashData(Encoding.UTF8.GetBytes(OldPassword)));

            if (HongtengDbCon.Db.Queryable<员工>().Any(it => it.手机 == phone && it.密码 == pwd))
            {
                if (NewPassword != RPassword)
                {
                    logz = "新密码两次输入的不同，请再次输入O(∩_∩)O~";
                }
                else
                {
                    string npwd = Convert.ToHexString(MD5.HashData(Encoding.UTF8.GetBytes(NewPassword)));

                    var result = HongtengDbCon.Db.Updateable<员工>()
                        .SetColumns(it => it.密码 == npwd)//SetColumns是可以叠加的 写2个就2个字段赋值
                        .Where(it => it.手机 == phone)
                        .ExecuteCommand();
                    if (result>0)
                    {
                        logz = "密码已经修改成功，请用新密码登录";

                        var uri = new Uri(NavigationManager.Uri);
                        var ReturnUrl = System.Web.HttpUtility.ParseQueryString(uri.Query)["ReturnUrl"];
                        NavigationManager.NavigateTo(string.IsNullOrWhiteSpace(ReturnUrl) ? "/Account/Login" : ReturnUrl);
                    }
                    else
                    {
                        logz = "密码未能修改";
                    }
                    
                }

            }
            else
            {
                logz = "旧密码不正确，请重新输入！";
            }

        }
        
    }
      
}
